home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / hiarcmen / textmenu.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  8.5 KB  |  233 lines  |  [TEXT/ttxt]

  1. in module TextMenuModule
  2.  
  3. --*******************************************************************************
  4. --*        Class name:    MenuButton
  5. --*                                            
  6. --*     Inherits from: TextPresenter and Actuator                                    
  7. --*        Class type: Concrete
  8. --*         Component: User Interface
  9. --*
  10. --*       Description: This is a subclass of TextPresenter and Actuator which 
  11. --*                    facilitates the creation of hierarchical text menus.
  12. --*                    Top level menus and submenus are created in very much
  13. --*                    the same way.  The only difference lies with the values
  14. --*                    of the supermenu and newMenu keywords.  For top level
  15. --*                    menus, set supermenu to UNDEFINED and newMenu to TRUE.
  16. --*                    For submenus, set supermenu to its parent menu and newMenu
  17. --*                    to FALSE.  To add new items (non menus) to the menu, use
  18. --*                    the addMenuItem method.
  19. --*
  20. --*             Usage: main menubutton -
  21. --*                        mb := new MenuButton supermenu:UNDEFINED \
  22. --*                                              label:"main menu"      \
  23. --*                                              newMenu:TRUE        \
  24. --*                                              width:100             \
  25. --*                                              height:20
  26. --*                    sub menubutton - 
  27. --*                        smb := new MenuButton supermenu:mb          \
  28. --*                                              label:"sub menu"    \
  29. --*                                              newMenu:FALSE         \
  30. --*                                              width:60             \
  31. --*                                              height:20
  32. --*                    menu items -
  33. --*                        addmenuitem supermenu label authordata activateAction
  34. --*
  35. --*            IVs:    authordata
  36. --*                    activateAction
  37. --*
  38. --*           Methods:    addMenuItem
  39. --*                    press
  40. --*                    release
  41. --*                    activate
  42. --*                    init
  43. --*                    
  44. --*    Required files:    none
  45. --*                    
  46. --*             Notes: If the action to be performed is a method, it must be 
  47. --*                    defined as a method of the authordata.
  48. --*                    
  49. --*                    Also refer to bmpmenu.sx. Notice the similarities in the
  50. --*                    way the 2 versions of MenuButton are implemented.
  51. --*
  52. --*            Author:    Su Quek - Kaleida Labs, Inc.
  53. --*******************************************************************************
  54. class MenuButton (TextPresenter, Actuator)
  55. inst vars
  56.     authordata
  57.     activateAction
  58. end                                         
  59.     
  60. --*=============================================================================*
  61. --*       Method name:    addMenuItem
  62. --*             Class:    MenuButton
  63. --*             Usage: addmenuitem self label authordata activateAction
  64. --*                        label        - String object
  65. --*                        authordata    - object
  66. --*                        activateAction    - function 
  67. --*             Notes: If the activateAction is a method, it must be defined as a 
  68. --*                    method of the authordata.
  69. --*-----------------------------------------------------------------------------*
  70. --*       Description: Adds a terminal button (one which does not invoke another
  71. --*                    menu) and sets its authordata and action.  
  72. --*=============================================================================*
  73. method addMenuItem self {class MenuButton} label authordata action -> 
  74. (
  75.     local menuItem := new MenuButton supermenu:self label:label newMenu:false 
  76.     menuItem.authordata := authordata
  77.     menuItem.activateAction := action
  78.  
  79.     return menuItem
  80. )
  81.  
  82. --*=============================================================================*
  83. --*       Method name:    press
  84. --*             Class:    MenuButton
  85. --*             Usage: press self
  86. --*-----------------------------------------------------------------------------*
  87. --*       Description: Sets the button to its pressed appearance.
  88. --*                    i.e. white letters on a black fill.  
  89. --*             Notes:    This method can be overridden to display a different
  90. --*                    pressed appearance.
  91. --*=============================================================================*
  92. method press self {class MenuButton} -> 
  93. (
  94.     nextmethod self
  95.     self.fill := blackBrush
  96.     self.stroke := blackBrush
  97.       setDefaultAttr self @brush whiteBrush
  98. )
  99.  
  100. --*=============================================================================*
  101. --*       Method name:    release
  102. --*             Class:    MenuButton
  103. --*             Usage: release self
  104. --*-----------------------------------------------------------------------------*
  105. --*       Description: Sets the button to its released appearance.
  106. --*                    i.e. black letters on a white fill. 
  107. --*             Notes:    This method can be overridden to display a different
  108. --*                    released appearance.
  109. --*=============================================================================*
  110. method release self {class MenuButton} -> 
  111. (
  112.     nextmethod self
  113.     self.fill := whiteBrush
  114.     self.stroke := blackBrush
  115.       setDefaultAttr self @brush blackBrush
  116. )
  117.  
  118. --*=============================================================================*
  119. --*       Method name:    activate
  120. --*             Class:    MenuButton
  121. --*             Usage: activate self
  122. --*-----------------------------------------------------------------------------*
  123. --*       Description: Sets the button to its released appearance.
  124. --*                    Executes the action to be performed when the button is 
  125. --*                    clicked (activated). 
  126. --*             Notes:    This method can be overridden to display a different
  127. --*                    released appearance.
  128. --*=============================================================================*
  129. method activate self {class MenuButton} -> 
  130. (
  131.     nextmethod self
  132.     
  133.     --*=========================================================================*
  134.     --* Change the button to its released appearance.
  135.     --*=========================================================================*
  136.     self.fill := whiteBrush
  137.     self.stroke := blackBrush
  138.       setDefaultAttr self @brush blackBrush
  139.       
  140.     --*=========================================================================*
  141.     --* Execute the action
  142.     --*=========================================================================*
  143.     if (self.activateAction <> undefined) do
  144.         self.activateAction self.authordata self
  145. )
  146.  
  147. --*=============================================================================*
  148. --*       Method name:    init
  149. --*             Class:    MenuButton
  150. --*             Usage: init self [supermenu:<MenuButton>]        
  151. --*                              [label:<String>]
  152. --*                              [newMenu:<Boolean>]
  153. --*                              [placement:<NameClass>] 
  154. --*                                    - (@menuDown, @menuRight, @menuAtPointer)    
  155. --*                              [width:<Number>]
  156. --*                              [height:<Number>]
  157. --*-----------------------------------------------------------------------------*
  158. --*       Description: Creates and initializes the TextPresenter to its default
  159. --*                    values unless another is explicitly given.
  160. --*                    Default values: 
  161. --*                        supermenu - (undefined) 
  162. --*                        label      - (" ")
  163. --*                        newMenu      - (true) 
  164. --*                        placement - (@menuDown) 
  165. --*                        width      - (50) 
  166. --*                        height      - (20)
  167. --*=============================================================================*
  168. method init self {class MenuButton} #rest args #key supermenu:(undefined) \
  169.                                                     label:(" ") \
  170.                                                     newMenu:(true) \
  171.                                                     placement:(@menuDown) \
  172.                                                     width:(50) \
  173.                                                     height:(20) ->
  174. (
  175.     apply nextmethod self boundary:(new rect x2:width y2:height) \
  176.                           target:label args
  177.                           
  178.     self
  179. )
  180.  
  181. --*=============================================================================*
  182. --*       Method name:    afterInit
  183. --*             Class:    MenuButton
  184. --*             Usage: afterInit self [supermenu:<MenuButton>]        
  185. --*                              [label:<String>]
  186. --*                              [newMenu:<Boolean>]
  187. --*                              [placement:<NameClass>] 
  188. --*                                    - (@menuDown, @menuRight, @menuAtPointer)    
  189. --*                              [width:<Number>]
  190. --*                              [height:<Number>]
  191. --*-----------------------------------------------------------------------------*
  192. --*       Description: Sets the appearance of the button. Adds the button to its 
  193. --*                    supermenu or creates it's submenu as appropriate.
  194. --*=============================================================================*
  195. method afterInit self {class MenuButton} #rest args #key supermenu:(undefined) \
  196.                                                     label:(" ") \
  197.                                                     newMenu:(true) \
  198.                                                     placement:(@menuDown) \
  199.                                                     width:(50) \
  200.                                                     height:(20) ->
  201. (
  202.     --*=========================================================================*
  203.     --* Set the appearance of the button.
  204.     --*=========================================================================*
  205.     self.fill := whiteBrush
  206.     self.stroke := blackBrush
  207.       setDefaultAttr self @brush blackBrush
  208.       
  209.       setDefaultAttr self @size 10
  210.       self.inset := new Point x:5 y:2
  211.     
  212.     --*=========================================================================*
  213.     --* Create a submenu for this button
  214.     --*=========================================================================*
  215.     if newMenu do 
  216.     (
  217.         local subMenu := new menu
  218.         subMenu.placement := placement
  219.         self.menu := subMenu
  220.     )
  221.  
  222.     --*=========================================================================*
  223.     --* Add this button to its supermenu
  224.     --*=========================================================================*
  225.     if (supermenu <> undefined) do
  226.         append supermenu.menu self
  227.         
  228.     return self                                  
  229. )
  230.  
  231. "Loaded hiermenu.sx"
  232.  
  233.